import axios from 'axios'
import Vue from 'vue'
import qs from 'qs'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = "http://biapi.luxsens.com/api";
// axios.defaults.withCredentials = true;

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    if (config.method == "post") {
    config.data = qs.stringify(config.data);
    }
  return config;
}, function (error) {
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response.data;
}, function (error) {
  return Promise.reject(error);
});

export function fetch(url, params, isBody) {
  return new Promise((resolve, reject) => {
    axios.post(url, params, {
        body: isBody
      })
      .then(response => {
        resolve(response);
      }, err => {
        reject(err);
      })
      .catch((error) => {
        reject(error);
      })
  })
}
export function getFetch(url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: params
      })
      .then(response => {
        resolve(response);
      }, err => {
        reject(err);
      })
      .catch((error) => {
        console.log(error)
      })
  })
}

Vue.prototype.$http = axios;
